<?php
namespace information\service;

/**--tmpl-use--**/

use common\service\CommonService as BaseService;

use herosphp\core\Loader;

/**--tmpl-import--**/

class CommonService extends BaseService{

    /**
     * 公告类型的上级菜单，限定分类为三级，所以查询二级菜单即可
     * @params $id 当前类型的id，
     * $levels 表示分类所处的等级,0表示一级，1表示二级，2表示三级
     */
    public function getParents($id=0)
    {
        if($id==0){
            $levels = 0;
        }else {
            $levels = $this->getlevels(array($id));
        }
        $lists_first = $this->modelDao->getItems(array('parent_id'=>0),array(),array('id'=>-1));//一级菜单
        $search_condi = array();
        foreach($lists_first as $list)
        {
            $search_condi[] = $list['id'];
        }
        $lists_second = $this->modelDao->getItems(array('parent_id'=>array('$in'=>$search_condi)),array(),array('id'=>-1));//二级菜单
        if($levels==0)
        {
            return array_merge($lists_first,$lists_second);
        }
        elseif($levels==1)
        {
            return $lists_first;
        }
        else
        {
            return array();
        }
    }

    /**
     * 获取子菜单的Ids
     * $paras $ids
     */
    public function getlevels($ids,$levels=0)
    {
        $items  = $this->modelDao->getItems(array('parent_id'=>array('$in'=>$ids)));
        if(!$items)
        {
            return $levels;
        }
        $childs = array();
        foreach($items as $item)
        {
            $childs[] = $item['id'];
        }
        $levels += 1;
        return $this->getlevels($childs,$levels);
    }
}

